package core.models;
import java.util.ArrayList;
import java.util.List;
import core.models.items.Item;
import java.util.Random;
import java.io.Serializable;

/**
 * Contains an ArrayList of Store's item inventory
 * @author 4c1a
 *
 */
public class Inventory implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Item> items;
	
	/**
	 * creates new inventory ArrayList
	 */
	public Inventory()
	{
		items = new ArrayList<Item>();
	}
	
	/**
	 * 
	 * @param items an ArrayList of Store's items
	 */
	public Inventory(List<Item> items)
	{
		 this.items = items;
	}
	
	/**
	 * Returns the inventory
	 * @return the ArrayList of inventory items
	 */
	public List<Item> getItems()
	{
		return items;
	}
	
	/**
	 * Adds an item to the inventory
	 * 
	 * @param toAdd item to be added to inventory ArrayList
	 */
	public void add(List<Item> toAdd)
	{
		if(toAdd == null)
			return;
		while(toAdd.size() > 0)
		{
			
			for(int j = 0; j <= items.size(); j++)
			{
				if (j == items.size())
				{
					items.add(toAdd.get(0));
					toAdd.remove(0);
					break;
				}
				else
				{
					if(items.get(j).getName().equals(toAdd.get(0).getName()))
					{
						items.get(j).setQuantity( items.get(j).getQuantity() + toAdd.get(0).getQuantity());
						toAdd.remove(0);
						break;
					}
				}
				
			}
		}
		
		clean();
	}
	
	/**
	 * 
	 * @param item
	 */
	public void add(Item item)
	{
		for(int i=0;i<items.size();i++)
		{
			if(items.get(i).getName()==item.getName())
			{
				items.get(i).setQuantity(items.get(i).getQuantity()+item.getQuantity());
				return;
			}
		}
		//if the item isn't in there 
		items.add(item);
	}
	
	/**
	 * Removes an item from the inventory
	 * 
	 * @param toRemove item to be removed from the inventory ArrayList
	 */
	public void remove(List<Item> toRemove)
	{
		if (toRemove == null)
			return;
		
		while(toRemove.size() > 0)
		{
			
			for(int j = 0; j <= items.size(); j++)
			{
				if (j == items.size())
				{
					toRemove.remove(0);
					break;
				}
				else
				{
					if(items.get(j).getName().equals(toRemove.get(0).getName()))
					{
						items.get(j).setQuantity(items.get(j).getQuantity() - toRemove.get(0).getQuantity());
						toRemove.remove(0);
						break;
					}
				}
				
			}
		}
		
	}
	public boolean eatFood(int eaten)
	{
		for (int i = 0; i < items.size(); i++)
		{
			if (items.get(i).getName().equals("Food"))
			{
				if (items.get(i).getQuantity() >= eaten)
				{
					items.get(i).setQuantity(items.get(i).getQuantity() - eaten);
					return true;
				}
				else
				{
					items.get(i).setQuantity(0);
					return false;
				}
			}
		}
		return false;
		
	}
	
	public int getItemQuantity(String item)
	{
		for (int i = 0; i < items.size(); i++)
		{
			if (items.get(i).getName().equals(item))
			{
				return items.get(i).getQuantity();
			}
		}
		return 0;
	}
	
	
	/**
	 * Deletes all nonExistant items from inventory
	 */
	public void clean()
	{
		for (int i = 0; i < items.size(); i++)
		{
			if (items.get(i).getQuantity() <= 0)
			{
				items.remove(i);
				i--;
			}
		}
	}
	
	/**
	 * Calculates and returns the total weight of all items in the inventory
	 * 
	 * @return all items' combined weight
	 */
	public int getTotalWeight()
	{
		int totalWeight = 0;
		for (int i = 0; i < items.size(); i++)
		{
			totalWeight += items.get(i).getWeight() * items.get(i).getQuantity();
		}
		return totalWeight;
	}
	
	/**
	 * returns a string representation of the inventory
	 */
	public String toString()
	{
		String stuff = "";
		for (int i = 0; i < items.size(); i++)
		{
			stuff += items.get(i).toString();
			stuff += "\n\n";
		}
		return stuff;
	}
	
	/***
	 * Drops a percentage of a random item's quantity
	 * 
	 * @return The item that has had a percentage of its inventory reduced
	 */
	public String dropRand(double percent)
	{
		Random rand = new Random();
		int index = (Math.abs(rand.nextInt()))%(items.size());
		if(index<0)
		{
			index = 0;
		}
		Item temp = items.get(index);
		if(temp.getName()=="Oxen")
		{
			index = 0;
			temp = items.get(0);
		}
		int initQuantity = temp.getQuantity();
		int afterLoss = (int)((double)(temp.getQuantity())*(percent));
		int numberLost = (initQuantity - afterLoss);
		temp.setQuantity(afterLoss);
		String returned = "You lost " + numberLost + " of " + temp.getName() + ".";
		return returned;
	}
	
}